from django.db import models
from .base import BaseModel

class Battle(BaseModel):
    """战斗模型"""
    name = models.CharField(max_length=100, verbose_name='战斗名称')
    description = models.TextField(blank=True, verbose_name='战斗描述')
    
    # 战斗状态
    STATUS_CHOICES = [
        ('preparing', '准备中'),
        ('in_progress', '进行中'),
        ('finished', '已结束'),
        ('paused', '已暂停'),
    ]
    status = models.CharField(
        max_length=20, 
        choices=STATUS_CHOICES, 
        default='preparing',
        verbose_name='战斗状态'
    )
    
    current_round = models.IntegerField(default=0, verbose_name='当前回合')
    current_character = models.ForeignKey(
        'Character',
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name='current_battles',
        verbose_name='当前行动角色'
    )
    
    # 新增：战斗创建者
    creator = models.ForeignKey(
        'accounts.User',
        on_delete=models.CASCADE,
        related_name='created_battles',
        verbose_name='创建者'
    )
    
    # 新增：战斗参与者
    participants = models.ManyToManyField(
        'accounts.User',
        related_name='participated_battles',
        verbose_name='参与者'
    )
    
    # 新增：是否为私密战斗
    is_private = models.BooleanField(default=False, verbose_name='是否私密')
    
    class Meta:
        verbose_name = '战斗'
        verbose_name_plural = '战斗'

class BattleCharacter(BaseModel):
    """战斗中的角色状态"""
    battle = models.ForeignKey(
        Battle,
        on_delete=models.CASCADE,
        related_name='battle_characters',
        verbose_name='所属战斗'
    )
    character = models.ForeignKey(
        'Character',
        on_delete=models.CASCADE,
        related_name='battle_states',
        verbose_name='角色'
    )
    
    # 位置信息
    position_x = models.FloatField(default=0, verbose_name='X坐标')
    position_y = models.FloatField(default=0, verbose_name='Y坐标')
    position_z = models.FloatField(default=0, verbose_name='Z坐标')
    
    # 战斗状态
    initiative_roll = models.IntegerField(verbose_name='先攻骰点')
    turn_order = models.IntegerField(verbose_name='行动顺序')
    has_acted = models.BooleanField(default=False, verbose_name='是否已行动')
    has_moved = models.BooleanField(default=False, verbose_name='是否已移动')
    has_reaction = models.BooleanField(default=True, verbose_name='是否可反应')
    
    # 临时状态效果
    temp_effects = models.JSONField(default=dict, verbose_name='临时效果')
    
    class Meta:
        verbose_name = '战斗角色状态'
        verbose_name_plural = '战斗角色状态'
        unique_together = ['battle', 'character'] 